package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;



    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> strsList = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettingList = new ArrayList<>();
            //把List<String[]> 转换为 List<OrderSetting>
            for (String[] strs : strsList) {
                //一个数组对应一个OrderSetting
                OrderSetting orderSetting = new OrderSetting();
                //把字符串日期转换为Date类型
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                Date orderDate = dateFormat.parse(strs[0]);
                //设置预约时间
                orderSetting.setOrderDate(orderDate);
                Integer number = Integer.parseInt(strs[1]);
                orderSetting.setNumber(number);
                //把预约设置对象添加到集合中
                orderSettingList.add(orderSetting);
            }
            //存储到数据库：调用service
            orderSettingService.addOrderSettingList(orderSettingList);
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/findByMonth")
    public Result findByMonth(String month){
        try {
            List<OrderSetting> orderSettingList = orderSettingService.findByMonth(month);
            /**
             * 获取的数据结构
             * [
             *      {orderDate:2019-11-02, number: 1 ,reservations : 1},
             *      {orderDate:2019-11-02, number: 1 ,reservations : 1},
             *       {orderDate:2019-11-02, number: 1 ,reservations : 1}
             * ]
             *
             * 需要的数据结构
             *
             * [
             //     { date: 21, number: 120, reservations: 120 },
             //     { date: 13, number: 120, reservations: 1 },
             //     { date: 24, number: 120, reservations: 120 },
             //     { date: 26, number: 120, reservations: 1 },
             //     { date: 28, number: 120, reservations: 1 }
             // ]
             */
            //转换成需要的数据
            List<Map<String,Object>> mapList = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                //一个orderSetting对象对应一个Map集合
                Map<String,Object> map = new HashMap<>();
                Date orderDate = orderSetting.getOrderDate();
                SimpleDateFormat dateFormat =  new SimpleDateFormat("dd");
                String date = dateFormat.format(orderDate);
                map.put("date", date );
                map.put("number", orderSetting.getNumber());
                map.put("reservations", orderSetting.getReservations());

                //把map添加到集合中
                mapList.add(map);
            }

            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS, mapList);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);

            orderSettingService.addOrderSettingList(orderSettingList);
            return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ORDERSETTING_FAIL);
        }
    }
}
